﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace QLC.Modules.TDHS.Components
{
    public partial class Item
    {
        #region Private members.

        protected int _ID;
        protected Guid _ModuleID = Guid.Empty;
        protected int _CategoryID;
        protected string _RegisterNumber = string.Empty;
        protected string _Status = string.Empty;
        protected string _RegisterName = string.Empty;
        protected string _IdentityNumber = string.Empty;
        protected string _Email = string.Empty;
        protected string _Phone = string.Empty;
        protected string _Address = string.Empty;
        protected string _Reply = string.Empty;
        protected DateTime _ReceivedDate = new DateTime(1753, 1, 1);
        protected DateTime _FinishedDate = new DateTime(1753, 1, 1);

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Properties.

        public int ID
        {
            set { this._ID = value; }
            get { return this._ID; }
        }

        public Guid ModuleID
        {
            set { this._ModuleID = value; }
            get { return this._ModuleID; }
        }

        public int CategoryID
        {
            set { this._CategoryID = value; }
            get { return this._CategoryID; }
        }

        public string RegisterNumber
        {
            set { this._RegisterNumber = value; }
            get { return this._RegisterNumber; }
        }

        public string Status
        {
            set { this._Status = value; }
            get { return this._Status; }
        }

        public string RegisterName
        {
            set { this._RegisterName = value; }
            get { return this._RegisterName; }
        }

        public string IdentityNumber
        {
            set { this._IdentityNumber = value; }
            get { return this._IdentityNumber; }
        }

        public string Email
        {
            set { this._Email = value; }
            get { return this._Email; }
        }

        public string Phone
        {
            set { this._Phone = value; }
            get { return this._Phone; }
        }

        public string Address
        {
            set { this._Address = value; }
            get { return this._Address; }
        }

        public string Reply
        {
            set { this._Reply = value; }
            get { return this._Reply; }
        }

        public DateTime ReceivedDate
        {
            set { this._ReceivedDate = value; }
            get { return this._ReceivedDate; }
        }

        public DateTime FinishedDate
        {
            set { this._FinishedDate = value; }
            get { return this._FinishedDate; }
        }


        #endregion

        //---------------------------------------------------------------------------------------------

        #region Select methods.

        public static Item Load(int id)
        {
            const string spName = "[dbo].[p_TDHS_Item_Load]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, id);
            Item entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new Item();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetGuid(reader.GetOrdinal("ModuleID"));
                if (!reader.IsDBNull(reader.GetOrdinal("CategoryID"))) entity.CategoryID = reader.GetInt32(reader.GetOrdinal("CategoryID"));
                if (!reader.IsDBNull(reader.GetOrdinal("RegisterNumber"))) entity.RegisterNumber = reader.GetString(reader.GetOrdinal("RegisterNumber"));
                if (!reader.IsDBNull(reader.GetOrdinal("Status"))) entity.Status = reader.GetString(reader.GetOrdinal("Status"));
                if (!reader.IsDBNull(reader.GetOrdinal("RegisterName"))) entity.RegisterName = reader.GetString(reader.GetOrdinal("RegisterName"));
                if (!reader.IsDBNull(reader.GetOrdinal("IdentityNumber"))) entity.IdentityNumber = reader.GetString(reader.GetOrdinal("IdentityNumber"));
                if (!reader.IsDBNull(reader.GetOrdinal("Email"))) entity.Email = reader.GetString(reader.GetOrdinal("Email"));
                if (!reader.IsDBNull(reader.GetOrdinal("Phone"))) entity.Phone = reader.GetString(reader.GetOrdinal("Phone"));
                if (!reader.IsDBNull(reader.GetOrdinal("Address"))) entity.Address = reader.GetString(reader.GetOrdinal("Address"));
                if (!reader.IsDBNull(reader.GetOrdinal("Reply"))) entity.Reply = reader.GetString(reader.GetOrdinal("Reply"));
                if (!reader.IsDBNull(reader.GetOrdinal("ReceivedDate"))) entity.ReceivedDate = reader.GetDateTime(reader.GetOrdinal("ReceivedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("FinishedDate"))) entity.FinishedDate = reader.GetDateTime(reader.GetOrdinal("FinishedDate"));
            }
            reader.Close();
            return entity;
        }

        //---------------------------------------------------------------------------------------------
        public static List<Item> SelectCollectionAll(Guid moduleID)
        {
            List<Item> collection = new List<Item>();
            SqlDataReader reader = (SqlDataReader)SelectReaderAll(moduleID);
            while (reader.Read())
            {
                Item entity = new Item();

                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetGuid(reader.GetOrdinal("ModuleID"));
                if (!reader.IsDBNull(reader.GetOrdinal("CategoryID"))) entity.CategoryID = reader.GetInt32(reader.GetOrdinal("CategoryID"));
                if (!reader.IsDBNull(reader.GetOrdinal("RegisterNumber"))) entity.RegisterNumber = reader.GetString(reader.GetOrdinal("RegisterNumber"));
                if (!reader.IsDBNull(reader.GetOrdinal("Status"))) entity.Status = reader.GetString(reader.GetOrdinal("Status"));
                if (!reader.IsDBNull(reader.GetOrdinal("RegisterName"))) entity.RegisterName = reader.GetString(reader.GetOrdinal("RegisterName"));
                if (!reader.IsDBNull(reader.GetOrdinal("IdentityNumber"))) entity.IdentityNumber = reader.GetString(reader.GetOrdinal("IdentityNumber"));
                if (!reader.IsDBNull(reader.GetOrdinal("Email"))) entity.Email = reader.GetString(reader.GetOrdinal("Email"));
                if (!reader.IsDBNull(reader.GetOrdinal("Phone"))) entity.Phone = reader.GetString(reader.GetOrdinal("Phone"));
                if (!reader.IsDBNull(reader.GetOrdinal("Address"))) entity.Address = reader.GetString(reader.GetOrdinal("Address"));
                if (!reader.IsDBNull(reader.GetOrdinal("Reply"))) entity.Reply = reader.GetString(reader.GetOrdinal("Reply"));
                if (!reader.IsDBNull(reader.GetOrdinal("ReceivedDate"))) entity.ReceivedDate = reader.GetDateTime(reader.GetOrdinal("ReceivedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("FinishedDate"))) entity.FinishedDate = reader.GetDateTime(reader.GetOrdinal("FinishedDate"));
                collection.Add(entity);
            }

            reader.Close();
            return collection;
        }

        //---------------------------------------------------------------------------------------------

        public static List<Item> SelectCollectionDynamic(string whereCondition, string orderByExpression)
        {
            List<Item> collection = new List<Item>();

            SqlDataReader reader = (SqlDataReader)SelectReaderDynamic(whereCondition, orderByExpression);
            while (reader.Read())
            {
                Item entity = new Item();

                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetGuid(reader.GetOrdinal("ModuleID"));
                if (!reader.IsDBNull(reader.GetOrdinal("CategoryID"))) entity.CategoryID = reader.GetInt32(reader.GetOrdinal("CategoryID"));
                if (!reader.IsDBNull(reader.GetOrdinal("RegisterNumber"))) entity.RegisterNumber = reader.GetString(reader.GetOrdinal("RegisterNumber"));
                if (!reader.IsDBNull(reader.GetOrdinal("Status"))) entity.Status = reader.GetString(reader.GetOrdinal("Status"));
                if (!reader.IsDBNull(reader.GetOrdinal("RegisterName"))) entity.RegisterName = reader.GetString(reader.GetOrdinal("RegisterName"));
                if (!reader.IsDBNull(reader.GetOrdinal("IdentityNumber"))) entity.IdentityNumber = reader.GetString(reader.GetOrdinal("IdentityNumber"));
                if (!reader.IsDBNull(reader.GetOrdinal("Email"))) entity.Email = reader.GetString(reader.GetOrdinal("Email"));
                if (!reader.IsDBNull(reader.GetOrdinal("Phone"))) entity.Phone = reader.GetString(reader.GetOrdinal("Phone"));
                if (!reader.IsDBNull(reader.GetOrdinal("Address"))) entity.Address = reader.GetString(reader.GetOrdinal("Address"));
                if (!reader.IsDBNull(reader.GetOrdinal("Reply"))) entity.Reply = reader.GetString(reader.GetOrdinal("Reply"));
                if (!reader.IsDBNull(reader.GetOrdinal("ReceivedDate"))) entity.ReceivedDate = reader.GetDateTime(reader.GetOrdinal("ReceivedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("FinishedDate"))) entity.FinishedDate = reader.GetDateTime(reader.GetOrdinal("FinishedDate"));
                collection.Add(entity);
            }

            reader.Close();
            return collection;
        }

        //---------------------------------------------------------------------------------------------

        // Select by foreign key return collection		
        public static IList<Item> SelectCollectionBy_CategoryID(int categoryID)
        {
            List<Item> collection = new List<Item>();
            SqlDataReader reader = (SqlDataReader)SelectReaderBy_CategoryID(categoryID);
            while (reader.Read())
            {
                Item entity = new Item();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetGuid(reader.GetOrdinal("ModuleID"));
                if (!reader.IsDBNull(reader.GetOrdinal("CategoryID"))) entity.CategoryID = reader.GetInt32(reader.GetOrdinal("CategoryID"));
                if (!reader.IsDBNull(reader.GetOrdinal("RegisterNumber"))) entity.RegisterNumber = reader.GetString(reader.GetOrdinal("RegisterNumber"));
                if (!reader.IsDBNull(reader.GetOrdinal("Status"))) entity.Status = reader.GetString(reader.GetOrdinal("Status"));
                if (!reader.IsDBNull(reader.GetOrdinal("RegisterName"))) entity.RegisterName = reader.GetString(reader.GetOrdinal("RegisterName"));
                if (!reader.IsDBNull(reader.GetOrdinal("IdentityNumber"))) entity.IdentityNumber = reader.GetString(reader.GetOrdinal("IdentityNumber"));
                if (!reader.IsDBNull(reader.GetOrdinal("Email"))) entity.Email = reader.GetString(reader.GetOrdinal("Email"));
                if (!reader.IsDBNull(reader.GetOrdinal("Phone"))) entity.Phone = reader.GetString(reader.GetOrdinal("Phone"));
                if (!reader.IsDBNull(reader.GetOrdinal("Address"))) entity.Address = reader.GetString(reader.GetOrdinal("Address"));
                if (!reader.IsDBNull(reader.GetOrdinal("Reply"))) entity.Reply = reader.GetString(reader.GetOrdinal("Reply"));
                if (!reader.IsDBNull(reader.GetOrdinal("ReceivedDate"))) entity.ReceivedDate = reader.GetDateTime(reader.GetOrdinal("ReceivedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("FinishedDate"))) entity.FinishedDate = reader.GetDateTime(reader.GetOrdinal("FinishedDate"));
                collection.Add(entity);
            }
            reader.Close();
            return collection;
        }
        //---------------------------------------------------------------------------------------------

        public static DataSet SelectBy_CategoryID(int categoryID)
        {
            const string spName = "[dbo].[p_TDHS_Item_SelectBy_CategoryID]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, categoryID);

            return db.ExecuteDataSet(dbCommand);
        }
        //---------------------------------------------------------------------------------------------

        public static DataSet SelectAll(Guid moduleID)
        {
            const string spName = "[dbo].[p_TDHS_Item_SelectAll]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
        {
            const string spName = "[dbo].[p_TDHS_Item_SelectDynamic]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderAll(Guid moduleID)
        {
            const string spName = "[dbo].[p_TDHS_Item_SelectAll]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
        {
            const string spName = "[dbo].[p_TDHS_Item_SelectDynamic]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        // Select by foreign key return collection		
        public static IDataReader SelectReaderBy_CategoryID(int categoryID)
        {
            const string spName = "p_TDHS_Item_SelectBy_CategoryID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, categoryID);

            return db.ExecuteReader(dbCommand);
        }
        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert methods.

        public static int InsertItem(Guid moduleID, int categoryID, string registerNumber, string status, string registerName, string identityNumber, string email, string phone, string address, string reply, DateTime receivedDate, DateTime finishedDate)
        {
            Item entity = new Item();
            entity.ModuleID = moduleID;
            entity.CategoryID = categoryID;
            entity.RegisterNumber = registerNumber;
            entity.Status = status;
            entity.RegisterName = registerName;
            entity.IdentityNumber = identityNumber;
            entity.Email = email;
            entity.Phone = phone;
            entity.Address = address;
            entity.Reply = reply;
            entity.ReceivedDate = receivedDate;
            entity.FinishedDate = finishedDate;
            return entity.Insert();
        }

        public int Insert()
        {
            return this.Insert(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Insert(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_TDHS_Item_Insert]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, ModuleID);
            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, CategoryID);
            db.AddInParameter(dbCommand, "@RegisterNumber", SqlDbType.VarChar, RegisterNumber);
            db.AddInParameter(dbCommand, "@Status", SqlDbType.NVarChar, Status);
            db.AddInParameter(dbCommand, "@RegisterName", SqlDbType.NVarChar, RegisterName);
            db.AddInParameter(dbCommand, "@IdentityNumber", SqlDbType.VarChar, IdentityNumber);
            db.AddInParameter(dbCommand, "@Email", SqlDbType.VarChar, Email);
            db.AddInParameter(dbCommand, "@Phone", SqlDbType.VarChar, Phone);
            db.AddInParameter(dbCommand, "@Address", SqlDbType.NVarChar, Address);
            db.AddInParameter(dbCommand, "@Reply", SqlDbType.NVarChar, Reply);
            db.AddInParameter(dbCommand, "@ReceivedDate", SqlDbType.DateTime, ReceivedDate.Year <= 1753 ? DBNull.Value : (object)ReceivedDate);
            db.AddInParameter(dbCommand, "@FinishedDate", SqlDbType.DateTime, FinishedDate.Year <= 1753 ? DBNull.Value : (object)FinishedDate);

            if (transaction != null)
            {
                db.ExecuteNonQuery(dbCommand, transaction);
                ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return ID;
            }
            db.ExecuteNonQuery(dbCommand);
            ID = (int)db.GetParameterValue(dbCommand, "@ID");
            return ID;
        }

        //---------------------------------------------------------------------------------------------
        public static bool InsertCollection(IList<Item> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (Item item in collection)
                        {
                            if (item.Insert(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at InsertCollection method: " + ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert / Update methods.

        public static int InsertUpdateItem(int id, Guid moduleID, int categoryID, string registerNumber, string status, string registerName, string identityNumber, string email, string phone, string address, string reply, DateTime receivedDate, DateTime finishedDate)
        {
            Item entity = new Item();
            entity.ID = id;
            entity.ModuleID = moduleID;
            entity.CategoryID = categoryID;
            entity.RegisterNumber = registerNumber;
            entity.Status = status;
            entity.RegisterName = registerName;
            entity.IdentityNumber = identityNumber;
            entity.Email = email;
            entity.Phone = phone;
            entity.Address = address;
            entity.Reply = reply;
            entity.ReceivedDate = receivedDate;
            entity.FinishedDate = finishedDate;
            return entity.InsertUpdate();
        }

        //---------------------------------------------------------------------------------------------

        public int InsertUpdate()
        {
            return this.InsertUpdate(null);
        }

        //---------------------------------------------------------------------------------------------

        public int InsertUpdate(SqlTransaction transaction)
        {
            const string spName = "p_TDHS_Item_InsertUpdate";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, ModuleID);
            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, CategoryID);
            db.AddInParameter(dbCommand, "@RegisterNumber", SqlDbType.VarChar, RegisterNumber);
            db.AddInParameter(dbCommand, "@Status", SqlDbType.NVarChar, Status);
            db.AddInParameter(dbCommand, "@RegisterName", SqlDbType.NVarChar, RegisterName);
            db.AddInParameter(dbCommand, "@IdentityNumber", SqlDbType.VarChar, IdentityNumber);
            db.AddInParameter(dbCommand, "@Email", SqlDbType.VarChar, Email);
            db.AddInParameter(dbCommand, "@Phone", SqlDbType.VarChar, Phone);
            db.AddInParameter(dbCommand, "@Address", SqlDbType.NVarChar, Address);
            db.AddInParameter(dbCommand, "@Reply", SqlDbType.NVarChar, Reply);
            db.AddInParameter(dbCommand, "@ReceivedDate", SqlDbType.DateTime, ReceivedDate.Year == 1753 ? DBNull.Value : (object)ReceivedDate);
            db.AddInParameter(dbCommand, "@FinishedDate", SqlDbType.DateTime, FinishedDate.Year == 1753 ? DBNull.Value : (object)FinishedDate);

            if (transaction != null)
            {
                return db.ExecuteNonQuery(dbCommand, transaction);
            }
            return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------
        public static bool InsertUpdateCollection(IList<Item> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (Item item in collection)
                        {
                            if (item.InsertUpdate(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at InsertUpdateCollection method: " + ex.Message);

                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }
        #endregion

        //---------------------------------------------------------------------------------------------

        #region Update methods.

        public static int UpdateItem(int id, Guid moduleID, int categoryID, string registerNumber, string status, string registerName, string identityNumber, string email, string phone, string address, string reply, DateTime receivedDate, DateTime finishedDate)
        {
            Item entity = new Item();
            entity.ID = id;
            entity.ModuleID = moduleID;
            entity.CategoryID = categoryID;
            entity.RegisterNumber = registerNumber;
            entity.Status = status;
            entity.RegisterName = registerName;
            entity.IdentityNumber = identityNumber;
            entity.Email = email;
            entity.Phone = phone;
            entity.Address = address;
            entity.Reply = reply;
            entity.ReceivedDate = receivedDate;
            entity.FinishedDate = finishedDate;
            return entity.Update();
        }

        //---------------------------------------------------------------------------------------------

        public int Update()
        {
            return this.Update(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Update(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_TDHS_Item_Update]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, ModuleID);
            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, CategoryID);
            db.AddInParameter(dbCommand, "@RegisterNumber", SqlDbType.VarChar, RegisterNumber);
            db.AddInParameter(dbCommand, "@Status", SqlDbType.NVarChar, Status);
            db.AddInParameter(dbCommand, "@RegisterName", SqlDbType.NVarChar, RegisterName);
            db.AddInParameter(dbCommand, "@IdentityNumber", SqlDbType.VarChar, IdentityNumber);
            db.AddInParameter(dbCommand, "@Email", SqlDbType.VarChar, Email);
            db.AddInParameter(dbCommand, "@Phone", SqlDbType.VarChar, Phone);
            db.AddInParameter(dbCommand, "@Address", SqlDbType.NVarChar, Address);
            db.AddInParameter(dbCommand, "@Reply", SqlDbType.NVarChar, Reply);
            db.AddInParameter(dbCommand, "@ReceivedDate", SqlDbType.DateTime, ReceivedDate.Year == 1753 ? DBNull.Value : (object)ReceivedDate);
            db.AddInParameter(dbCommand, "@FinishedDate", SqlDbType.DateTime, FinishedDate.Year == 1753 ? DBNull.Value : (object)FinishedDate);

            if (transaction != null)
            {
                return db.ExecuteNonQuery(dbCommand, transaction);
            }
            return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------
        public static bool UpdateCollection(IList<Item> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (Item item in collection)
                        {
                            if (item.Update(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at UpdateCollection method: " + ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Delete methods.

        public static int DeleteItem(int id)
        {
            Item entity = new Item();
            entity.ID = id;

            return entity.Delete();
        }

        public int Delete()
        {
            return this.Delete(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Delete(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_TDHS_Item_Delete]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, ID);

            if (transaction != null)
            {
                return db.ExecuteNonQuery(dbCommand, transaction);
            }
            return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static int DeleteBy_CategoryID(int categoryID)
        {
            const string spName = "[dbo].[p_TDHS_Item_DeleteBy_CategoryID]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, categoryID);

            return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static bool DeleteCollection(IList<Item> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (Item item in collection)
                        {
                            if (item.Delete(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at DeleteCollection method: " + ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }
        #endregion
    }
}